package com.publicize.controller;


import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.Module;
import com.publicize.entity.Relationship;
import com.publicize.entity.Vo.QueryVo;
import com.publicize.service.ModuleService;
import com.publicize.service.RelationshipService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 投稿信息表 前端控制器
 * </p>
 *
 * @author bs
 * @since 2021-03-03
 */
@RestController
@RequestMapping("/backstage/module")
@CrossOrigin
public class ModuleController {
    @Resource
    private ModuleService moduleService;

    @Resource
    private RelationshipService relationshipService;

    //查询所有模块
    @GetMapping("selectRelationship")
    public Result selectRelationship(){
        List<Relationship> list = relationshipService.list(null);
        return Result.ok().data("relationships",list);
    }
    //根据稿子id查询对应稿子
    @GetMapping("selectModuleById/{id}")
    public Result selectModuleById(@PathVariable("id") String id){
        Module module = moduleService.getById(id);
        return Result.ok().data("module",module);
    }

    //shan
    @PostMapping("deleteModuleById/{id}")
    public Result deleteModuleById(@PathVariable("id") String id){
        moduleService.removeById(id);
        return Result.ok();
    }

    //根据模块id查询对应未审核稿子
    @GetMapping("selectRelationshipId/{relationshipId}/{page}/{limit}")
    public Result selectModuleById(@PathVariable("relationshipId") String relationshipId,
                                   @PathVariable("page") Integer page,
                                   @PathVariable("limit") Integer limit
    ){
        Result list=moduleService.selectModuleById(relationshipId,page,limit);
        return list;
    }
    //后台管理投稿
    @PostMapping("addModule")
    public Result addModule(@RequestBody Module module, HttpServletRequest request){
        System.out.println(module);
        String manageId = JwtUtils.getMemberIdByJwtToken(request);
        module.setUserId(manageId);
        String id=getRandom620(20);
        module.setId(id);
        boolean save = moduleService.save(module);
        return save==true?Result.ok().data("id",id): Result.error();
    }
    //修改稿子
    @PostMapping("updateModule")
    public Result updateModule(@RequestBody Module module){
        boolean update = moduleService.update(module, null);
        return update==true?Result.ok(): Result.error();
    }

    /**
     *
     * @param id
     * @param isCheck 通过(0) 未通过(1)
     * @return
     */
    //设置审核
    @PostMapping("statusManage/{id}/{status}")
    public Result statusManage(@PathVariable("id")String id,@PathVariable("status") Integer status){
        moduleService.statusManage(id,status);
        return Result.ok();
    }
    //高级查询模块
    /**
     *  id 根据稿子id
     * title 根据标题
     * username 根据用户名
     * startTime-endTime  根据投稿时间
     */
    @PostMapping("selectByCondition")
    public Result selectByCondition(@RequestBody QueryVo queryVo){
        Map<String, Object> resultMap = moduleService.selectByConditions(queryVo);
        return Result.ok().data("resultMap",resultMap);
    }

    //生成随机数
    public static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);

            result += randInt;
        }
        return result;
    }

    @PostMapping("test")
    public Result test(@RequestBody Module module){
        String data=getRandom620(20);
        module.setId(data);
        System.out.println(data);
        moduleService.save(module);
        return Result.ok().data("data",data);
    }
}

